﻿using DingTalk.Core;
using DingTalk.Core.Models;
using DingTalk.Corp.Message.Configurations;
using DingTalk.Corp.Message.Models;
using MiniFox.Components;
using MiniFox.Web;

namespace DingTalk.Corp.Message.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class DingTalkAgentMessageService : DingTalkAgentService, IDingTalkAgentMessageService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        public DingTalkAgentMessageService(string name) : base(name)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="setting"></param>
        public DingTalkAgentMessageService(IDingTalkAgent setting) : base(setting)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        [AutoConfigure]
        protected DingTalkCorpMessageConfiguration MessageConfiguration { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public Uri MessageSendUri
        {
            get
            {
                return new Uri(MessageConfiguration.MessageSendUri);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public Uri MessageResultUri
        {
            get
            {
                return new Uri(MessageConfiguration.MessageResultUri);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public Uri MessageStatusUpdateUri
        {
            get
            {
                return new Uri(MessageConfiguration.MessageStatusUpdateUri);
            }
        }


        #region message

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        protected virtual MessageRequest CreateMessageRequest(DingTalkMessage message, params string[] userIds)
        {
            return new MessageRequest(AgentID, message) { UserIDs = userIds };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userIds"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public virtual async Task<MessageResponse> SendMessageAsync(DingTalkMessage message, params string[] userIds)
        {
            var uri = this.MessageSendUri.AddQueryString(ApiGlobalKey.ACCESS_TOKEN, this.AccessToken);
            var request = CreateMessageRequest(message, userIds);
            return await this.HttpPostAsync<MessageRequest, MessageResponse>(uri, request);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="taskId"></param>
        /// <param name="statusVal"></param>
        /// <param name="bgColor"></param>
        /// <returns></returns>
        public async Task<StandardResponse> UpdateMessageAsync(string taskId, string statusVal, string bgColor)
        {
            var uri = this.MessageStatusUpdateUri.AddQueryString(ApiGlobalKey.ACCESS_TOKEN, this.AccessToken);
            var request = new UpdateMessageRequest
            {
                AgentID = AgentID,
                TaskID = taskId,
                StatusValue = statusVal,
                StatusBgColor = bgColor
            };
            return await this.HttpPostAsync<UpdateMessageRequest, StandardResponse>(uri, request);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public async Task<MessageResultResponse> GetMessageResultAsync(string taskId)
        {
            var url = this.MessageResultUri.AddQueryString(ApiGlobalKey.ACCESS_TOKEN, this.AccessToken);
            var request = new
            {
                agent_id = this.AgentID,
                task_id = taskId,
            };
            return await this.HttpPostAsync<object, MessageResultResponse>(url, request);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public virtual Task<MessageResponse> SendTextMessageAsync(string content, params string[] userIds)
        {
            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentException($"“{nameof(content)}”不能为 null 或空。", nameof(content));
            }

            if (userIds is null)
            {
                throw new ArgumentNullException(nameof(userIds));
            }

            return this.SendMessageAsync(new TextMessage(content), userIds);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="text"></param>
        /// <param name="messageUri"></param>
        /// <param name="pictureUri"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public Task<MessageResponse> SendLinkMessageAsync(string title, string text, string messageUri, string pictureUri, params string[] userIds)
        {
            return this.SendMessageAsync(new LinkMessage(title, text, messageUri, pictureUri), userIds);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="headTitle"></param>
        /// <param name="bodyTitle"></param>
        /// <param name="bodyContent"></param>
        /// <param name="messageUri"></param>
        /// <param name="imageId"></param>
        /// <param name="statusValue"></param>
        /// <param name="statusColor"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public Task<MessageResponse> SendOAMessageAsync(string headTitle, string bodyTitle, string bodyContent, string messageUri, string imageId, string statusValue, string statusColor, params string[] userIds)
        {
            return SendOAMessageAsync(headTitle, bodyTitle, bodyContent, null, messageUri, imageId, statusValue, statusColor, userIds);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="headTitle"></param>
        /// <param name="bodyTitle"></param>
        /// <param name="bodyContent"></param>
        /// <param name="formData"></param>
        /// <param name="messageUri"></param>
        /// <param name="imageId"></param>
        /// <param name="statusValue"></param>
        /// <param name="statusColor"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public Task<MessageResponse> SendOAMessageAsync(string headTitle, string bodyTitle, string bodyContent, IDictionary<string, string> formData, string messageUri, string imageId, string statusValue, string statusColor, params string[] userIds)
        {
            var message = new OAMessage
            {
                OA = new OAData
                {
                    Header = new OAHeader
                    {
                        BgColor = "FF004ea1",
                        Text = headTitle
                    },
                    Body = new OABody
                    {
                        Title = bodyTitle,
                        Content = bodyContent,
                        FormData = formData == null ? null : formData,
                        Image = imageId
                    },
                    MessageUrl = messageUri,
                    StatusBar = new OAStatusBar
                    {
                        StatusBgColor = statusColor,
                        StatusValue = statusValue
                    }
                }
            };
            return this.SendMessageAsync(message, userIds);
        }

        #endregion
    }
}
